<?php

// +----------------------------------------------------------------------
// | LuluCWS [ Lulu COMPANY WEB SHOW]
// +----------------------------------------------------------------------
// | Copyright (c) 2010 http://www.luluui.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: uuleaf <uuleaf@163.com>
// +----------------------------------------------------------------------
class SettleModel extends IrelaModel {
	/**
	 +----------------------------------------------------------
	 * 关联设置
	 +----------------------------------------------------------
	 * @access public
	 +----------------------------------------------------------
	 * @param none
	 +----------------------------------------------------------
	 * @return none
	 +----------------------------------------------------------
	 * @throws none
	 +----------------------------------------------------------
	 */
	protected $_link = array (
		'divide' => array (
			'mapping_type' => HAS_MANY,
			'class_name' => 'Divide',
			'foreign_key' => 'settle_id',
			'mapping_name' => 'divide',
			'mapping_order' => 'orderform_overed desc',
		)
	);
	/**
	 +----------------------------------------------------------
	 * 字段设置数组
	 +----------------------------------------------------------
	 * @access public
	 +----------------------------------------------------------
	 * @param none
	 +----------------------------------------------------------
	 * @return none
	 * '字段名称' => array();
	 * t 字体类型 ：input text function select relation
	 * v 默认值
	 +----------------------------------------------------------
	 * @throws ThinkExecption
	 +----------------------------------------------------------
	 */
	public $fields_set = array (
		'settle_id' => array (
			't' => 'input',
			'v' => 0
		),
		'year' => array (
			't' => 'input',
			'v' => 0
		),
		'month' => array (
			't' => 'input',
			'v' => 0
		),
		'day' => array (
			't' => 'input',
			'v' => 1
		),
		'is_settled' => array (
			't' => 'input',
			'v' => 0
		),
		'settled' => array (
			't' => 'input',
			'v' => 0
		),
		'fee' => array (
			't' => 'input',
			'v' => 0
		)
	);

	public function settling() {
		$marray = $this->check_settle();
		//dump($marray);
		if ($marray)
			$this->do_settles($marray);
	}
	/**
	 +----------------------------------------------------------
	 * 判断结算必要
	 +----------------------------------------------------------
	 * @access protected
	 +----------------------------------------------------------
	 * @param none
	 +----------------------------------------------------------
	 * @return 
	 * false:不用结算
	 * array(): y=>年 m=>月 结算的年月
	 +----------------------------------------------------------
	 * @throws none
	 +----------------------------------------------------------
	 */

	public function check_settle() {
		$marray = array ();
		//取得结算时间
		$msiteinfo = M('Config');
		$map['name'] = 'settle_date';
		$settle_day = $msiteinfo->where($map)->getField('val');
		if (!$settle_day)
			$settle_day = 10;

		$settle_day = (int) $settle_day;
		//取得当前年月份
		$nowyear = (int) date('y');
		$nowmonth = (int) date('m');
		$nowday = (int) date('d');

		//取得最近已结算记录
		$lastinfo = $this->order('settled desc')->find();
		$lastyear = 0;
		$lastmonth = 0;
		if ($lastinfo) {
			$lastyear = $lastinfo['year'];
			$lastmonth = $lastinfo['month'] + 1;
			//如果无结算记录，本月是否需结算，否则现在就结算
		} else {
			$is_nowmonth = ($nowday > $settle_day);
			//echo $nowday.$settle_day;
			$marray[] = array (
				'y' => $nowyear,
				'm' => $nowmonth -1,
			);
			if ($is_nowmonth)
				return $marray;

			return false;
		}

		//如果同年同月
		if ($nowyear == $lastyear && $nowmonth == $lastmonth)
			return false;
		//如果不是同一年份
		if ($nowyear > $lastyear) {
			while ($lastyear <= $nowyear) {
				//echo $lastyear.$nowyear;
				while ($lastmonth <= 12) {
					$marray[] = array (
						'y' => $lastyear,
						'm' => $lastmonth,
					);
					$lastmonth++;
				}
				$lastmonth = 1;
				$lastyear++;
			}
			$lastmonth = 1;
		}
		//如果是同一年份
		if ($nowyear == $lastyear) {
			//如果不同月份
			while ($lastmonth < $nowmonth) {
				$marray[] = array (
					'y' => $lastyear,
					'm' => $lastmonth
				);
				$lastmonth++;
			}
		}

		if (empty ($marray))
			return false;
		return $marray;
	}
	public function do_settle($year, $month) {
		if ($month == 0) {
			$year--;
			$month = 12;
		}
		//echo 'Y:'.$year.'m'.$month;
		//初始化基本信息
		//settle:settle_id year month is_settled settled fee 
		$datainfo = array (
			'settle_id' => '',
			'year' => $year,
			'month' => $month,
			'day' => date('d'),
			'is_settled' => 0,
			'settled' => time(),
			'fee' => 0,
		);
		//从commission中提取数据
		$mcommssion = M('Divide');
		$this->startTrans();
		//commission数据过滤条件
		$cmap['orderform_ok'] = 1; //交易成功
		$cmap['is_settled'] = 0; //未统计结算
		$totime = mktime(0, 0, 0, $month +1, 0, $year);
		$cmap['orderform_overed'] = array (
			'lt',
			$totime
		); //时间小于月底
		$comlist = $mcommssion->where($cmap)->order('divide_id desc')->select();
		if (!$comlist)
			return false;
		foreach ($comlist as $co) {
			$datainfo['fee'] += $co['fee'];
		}
		//dump($datainfo);
		if ($datainfo['fee'] == 0)
			return false;
		//标记divide_commission已经统计
//		if (!$mcommssion->where($cmap)->setField(array (
//				'is_settled',
//				'settled'
//			), array (
//				1,
//				time()
//			))) {
//			$this->rollback();
//			return false;
//		}
		$settle_id = $this->add($datainfo);
		if (!$settle_id) {
			$this->rollback();
			return false;
		}
		$mcommssion->setField('settle_id',$settle_id,$cmap);
		$this->commit();
	}
	
	public function do_settles($marray) {
		//dump($marray);
		foreach ($marray as $am) {
			$this->do_settle($am['y'], $am['m']);
		}
	}
	public function divide($settle_id = 0, $divide_id = 0) {
		$msm = M('Divide');
		if (!$settle_id || !$divide_id)
			return false;
		$map['settle_id'] = $settle_id;
		$map['divide_id'] = $divide_id;
		$suinfo = $msm->where($map)->find();
		if (!$msm->where($map)->setField(array (
				'is_settled',
				'settled'
			), array (
				1,
				time()
			)))
			return false;
		unset ($map['divide_id']);
		$map['is_settled'] = 0;
		$unsettlednum = $msm->where($map)->count();
		if (!$unsettlednum)
			$this->where($map)->setField(array (
				'is_settled',
				'settled'
			), array (
				1,
				time()
			));
		return true;
	}
}
?>